import { isInArray } from "@/utils"

export default class CreepBaseExtension extends Creep
{
    /* Creep原型扩展 */

    /* ----------------------------基础函数接口------------------------------ */
    // 寻路规则为最简便的moveTo
    public harvest_(resource_:Source) : void{
        if (this.harvest(resource_) == ERR_NOT_IN_RANGE)
        {
            this.moveTo(resource_,{reusePath:15})
        }
    }

    public withdraw_(distination:Structure,rType:ResourceConstant = RESOURCE_ENERGY) : void{
        if (this.withdraw(distination,rType) == ERR_NOT_IN_RANGE)
        {
            this.moveTo(distination,{reusePath:15})
        }
    }

    public transfer_(distination:Structure,rType:ResourceConstant = RESOURCE_ENERGY) : void{
        if (this.transfer(distination,rType) == ERR_NOT_IN_RANGE)
        {
            this.moveTo(distination,{reusePath:15})
            //{visualizePathStyle: {stroke: '#aaaaaa'},ignoreCreeps: true}
        }
    }

    public upgrade_():void{
        if (this.room.controller)
            if (this.upgradeController(this.room.controller) == ERR_NOT_IN_RANGE)
                this.moveTo(this.room.controller,{reusePath:15})
    }

    public build_(distination:ConstructionSite) : void {
        if  (this.build(distination) == ERR_NOT_IN_RANGE)
            this.moveTo(distination,{visualizePathStyle: {stroke: '#ffffff'},reusePath:15})
    }
    public repair_(distination:Structure) : void {
        if  (this.repair(distination) == ERR_NOT_IN_RANGE)
            this.moveTo(distination,{reusePath:15})
    }

    
    // 寻路规则为rush接口 （无视creep并且允许对穿，但是在离目标一定范围后，将不再允许对穿）
    public harvest__(source_:Source):void{
        if (this.harvest(source_) == ERR_NOT_IN_RANGE)
        {
            this.goTo(source_.pos,1)
            this.memory.standed = false
        }
        else this.memory.standed = true

    }

    public transfer__(distination:Structure,rType:ResourceConstant = RESOURCE_ENERGY) : void{
        if (this.transfer(distination,rType) == ERR_NOT_IN_RANGE)
        {
            this.rush(distination.pos)
        }
        this.memory.standed = false
    }

    public upgrade__():void{
        if (this.room.controller)
        {
            if (this.upgradeController(this.room.controller) == ERR_NOT_IN_RANGE) 
            {
                this.goTo(this.room.controller.pos,1)
                this.memory.standed = false
            }
            else this.memory.standed = true
        }
    }
    // 考虑到建筑和修复有可能造成堵虫，所以解除钉扎状态
    public build__(distination:ConstructionSite) : void {
        if  (this.build(distination) == ERR_NOT_IN_RANGE)
        {
            this.goTo(distination.pos,1)
            this.memory.standed = false
        }
        else
        this.memory.standed = true
    }
    public repair__(distination:Structure) : void {
        if (this.repair(distination) == ERR_NOT_IN_RANGE)
        {
            this.goTo(distination.pos,1)
            this.memory.standed = false
        }
        else
            this.memory.standed = true
    }
    
    public withdraw__(distination:Structure,rType:ResourceConstant = RESOURCE_ENERGY) : void{
        if (this.withdraw(distination,rType) == ERR_NOT_IN_RANGE)
        {
            this.goTo(distination.pos,1)
        }
        this.memory.standed = false
    }



    // 前往旗帜处
    public moveFlag(FlagName:string):void
    {
        this.memory.standed = true
        if (Game.flags[FlagName])
        this.rush(Game.flags[FlagName].pos)
    }

    // 当爬虫生命低于指定tick时，将身上所有资源运送到storage_里,运送完自杀
    public LastMisson(num:number):void{
        if (this.ticksToLive <= num)
        {
            var storage_ = this.pos.getClosestStructure([STRUCTURE_STORAGE],0) as StructureStorage
            if (!storage_) this.suicide
            var st = this.store
            for (var i of Object.keys(st))
            {
                this.transfer__(storage_,i as ResourceConstant)
                return
            }
            if (Object.keys(st).length <= 0)
                this.suicide()
        }
    }

    // 获取爬虫Body属性中指定属性的数量
    public GetBodyArray(part:BodyPartConstant[]):BodyPartConstant[]{
        var result:BodyPartConstant[] = []
        for (var i of this.body)
        {
            if(isInArray(part,i.type))
                result.push(i.type)
        }
        return result
    }
    // 确认指定爬虫指定body是否全部强化完毕
    public CheckBoost(part:BodyPartConstant[]):boolean{
        for (var i of this.body)
        {
            if(isInArray(part,i.type))
            {
                if (!i.boost)
                return false
            }
        }
        return true
    }
}